using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The PublicHolidayCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about PublicHolidayCodeTable objects.
/// </summary>
public class PublicHolidayCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of PublicHolidayCodeTable from the underlying datasource.
/// </summary>
/// <param name="publicHolidayCode">The unique PublicHolidayCode of the PublicHolidayCodeTable in the database.</param>
/// <returns>An PublicHolidayCodeTable when the PublicHolidayCode was found in the database, or null otherwise.</returns>
public static PublicHolidayCodeTable GetItem(int publicHolidayCode)
{
PublicHolidayCodeTable myPublicHolidayCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocPublicHolidayCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@publicHolidayCode", publicHolidayCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myPublicHolidayCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myPublicHolidayCodeTable;
}
}

/// <summary>
/// Returns a list with PublicHolidayCodeTable objects.
/// </summary>
/// <returns>A generics List with the PublicHolidayCodeTable objects.</returns>
public static PublicHolidayCodeTableList GetList()
{
PublicHolidayCodeTableList tempList = new PublicHolidayCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocPublicHolidayCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a PublicHolidayCodeTable in the database.
/// </summary>
/// <param name="myPublicHolidayCodeTable">The PublicHolidayCodeTable instance to save.</param>
/// <returns>The new PublicHolidayCode if the PublicHolidayCodeTable is new in the database or the existing PublicHolidayCode when an item was updated.</returns>
public static int Save(PublicHolidayCodeTable myPublicHolidayCodeTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocPublicHolidayCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myPublicHolidayCodeTable.PublicHolidayCode == -1)
{myCommand.Parameters.AddWithValue("@publicHolidayCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@publicHolidayCode", myPublicHolidayCodeTable.PublicHolidayCode);
}
if (myPublicHolidayCodeTable.CalenderDate == null){
myCommand.Parameters.AddWithValue("@calenderDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@calenderDate", myPublicHolidayCodeTable.CalenderDate);
}
if (string.IsNullOrEmpty(myPublicHolidayCodeTable.Description))
{
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myPublicHolidayCodeTable.Description);
}
if (string.IsNullOrEmpty(myPublicHolidayCodeTable.Remark))
{
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", myPublicHolidayCodeTable.Remark);
}
if (myPublicHolidayCodeTable.IsRepeat == null){
myCommand.Parameters.AddWithValue("@isRepeat", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@isRepeat", myPublicHolidayCodeTable.IsRepeat);
}
if (myPublicHolidayCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myPublicHolidayCodeTable.Status);
}
if (myPublicHolidayCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myPublicHolidayCodeTable.CreatedDateTime);
}
if (myPublicHolidayCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myPublicHolidayCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a PublicHolidayCodeTable from the database.
/// </summary>
/// <param name="publicHolidayCode">The PublicHolidayCode of the PublicHolidayCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int publicHolidayCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocPublicHolidayCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@publicHolidayCode", publicHolidayCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the PublicHolidayCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static PublicHolidayCodeTable FillDataRecord(IDataRecord myDataRecord )
{
PublicHolidayCodeTable myPublicHolidayCodeTable = new PublicHolidayCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PublicHolidayCode")))
{
myPublicHolidayCodeTable.PublicHolidayCode = myDataRecord.GetInt32(myDataRecord.GetOrdinal("PublicHolidayCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CalenderDate")))
{
myPublicHolidayCodeTable.CalenderDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CalenderDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myPublicHolidayCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
myPublicHolidayCodeTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("IsRepeat")))
{
myPublicHolidayCodeTable.IsRepeat = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("IsRepeat"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myPublicHolidayCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myPublicHolidayCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myPublicHolidayCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myPublicHolidayCodeTable;
}
}

 } 
